const mysql = require('mysql');
const {
	getMSTime,
} = require('../../utils/dateUtils')
const {
	selectOne,
	select,
	insert,
	insertBatch,
	update,
	remove,
	executeSQL,
	strip,
} = require('./mysql-client')

// const options = {
// 	// host: "192.168.1.3",
// 	host: "192.168.1.2",
// 	user: "root",
// 	port: 3306,
// 	// password: "atmd@dd_2015",
// 	password: "atmd@dd_2014",
// 	database: "songlist",
// 	charset: "utf8_general_ci"
// }

module.exports = class MySQL {
	constructor(options) {
		this.__options = options
		this.__init()
	}

	__initMethod() {
		this.selectOne = selectOne.bind(this)
		this.select = select.bind(this)
		this.insert = insert.bind(this)
		this.insertBatch = insertBatch.bind(this)
		this.update = update.bind(this)
		this.remove = remove.bind(this)
		this.executeSQL = executeSQL.bind(this)
		this.strip = strip
	}

	/**
	 * 获取mysql连接，断开后自动重新获取
	 * @return {[type]} [description]
	 */
	__init() {
		this.__options.connectionLimit = this.__options.connectionLimit || 30
		this.__pool = mysql.createPool(this.__options);
		this.__initMethod()
	}

	__rebuildPool() {
		if (this.__pool) {
			this.__pool.end(err => {
				console.log('---MYSQL POOL END ERROR---', err)
			})
		}
		this.__pool = null
		this.__init()
	}
}